function gadget:GetInfo()
return {
		name = "healthsubsystems.lua",
		desc = "adds gather command",
		author = "tomr13",
		date = "10/11/2011",
		license = "public domain",
		layer = 1,
		enabled = true,
}
end

local workerTypes={}
workerTypes[UnitDefNames["gatherer"].id]={
  capacity=5,
  gatherTime=1,
}


local hqTypes={}

hqTypes[UnitDefNames["simplefactory"].id]={
  returnRange=60
}
--endconfig
local echo = Spring.Echo


local Ships = {
	[UnitDefNames.gatherer.id] = "gatherer",
	[UnitDefNames.heavyattackship.id] = "heavyattackship",
	[UnitDefNames.lightattackship.id] = "lightattackship",
}
local CMD_MAX_HEALTH_SUBSYSTEM = 33637

function gadget:UnitPreDamaged(unitID, unitDefID, unitTeam, damage, paralyzer, weaponID, attackerID, attackerDefID, attackerTeam)
	if(Spring.GetUnitRulesParam(unitID,"Health_Subsystem")) then
		--echo(" Unit ID: " .. unitID .." Shields" .. Spring.GetUnitRulesParam(attackerID,"Health_Subsystem"))
		if(Spring.GetUnitRulesParam(unitID,"Health_Subsystem") < 100) then
			return damage * (100-Spring.GetUnitRulesParam(unitID,"Health_Subsystem")) / 100.0
		end
	end
	return damage
end



if not GG.attUnits then
	GG.attUnits = {}
end

local units = {}

Health_Subsystem_Toggle = {
		id=CMD_MAX_HEALTH_SUBSYSTEM,
		type=CMDTYPE.ICON,
			name="Shields",
			texture="&.9x.9&bitmaps/icons/blank.tif&bitmaps/icons/burrow.png",
			tooltip="Gathers wreckage.",
			action="healthsubsystem"
		}

if (not gadgetHandler:IsSyncedCode()) then 
 return false
end

local function ChangeSubSystems(unitID, health, movement, damage)
	--Spring.Echo("HELLO")
	Spring.SetUnitRulesParam(unitID,"Health_Subsystem", health)
	
	Spring.SetUnitRulesParam(unitID,"Movement_Subsystem", movement)
	Spring.SetUnitRulesParam(unitID,"Damage_Subsystem", damage)
	
	local a = Spring.GetUnitRulesParam(unitID,"Health_Subsystem")
	local b = Spring.GetUnitRulesParam(unitID,"Movement_Subsystem")
	local c = Spring.GetUnitRulesParam(unitID,"Damage_Subsystem")
	--Spring.Echo("Subsystems Set: Health:  " .. a .. "   Movement: " .. b .. "  Damage: " .. c)
end

function gadget:CommandFallback(unitID, unitDefID, teamID, cmdID, cmdParams, cmdOptions)
 if (cmdID~=CMD_MAX_HEALTH_SUBSYSTEM) then
  return false
 end
 if(not Ships[unitDefID]) then
   return true, true --this shouldn't happen and I'm not sure but I think that this removes the command from the list
 end
 ChangeSubSystems(unitID,100,1,1)
end


function gadget:UnitDestroyed(unitID, unitDefID, team)
	if(Ships[unitDefID]) then
		table.remove(units,unitID)
	end
end

function gadget:UnitCreated(unitID, unitDefID, unitTeam)
	if(Ships[unitDefID]) then
		Spring.InsertUnitCmdDesc(unitID, Health_Subsystem_Toggle)
		ChangeSubSystems(unitID,100,100,100)
		table.insert(units, unitID)
	end
end

function gadget:Initialize()
 gadgetHandler:RegisterCMDID(CMD_MAX_HEALTH_SUBSYSTEM)
end